/**
 * 
 */
package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.AuditRecord;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VAuditRecord;
import com.jxc.service.AuditRecordServiceI;

/**
 * @author Tino
 * 
 */
@Service("auditRecordService")
public class AuditRecordServiceImpl implements AuditRecordServiceI {

	@Autowired
	private BaseDaoI<AuditRecord> auditRecordDao;

	@Override
	public List<VAuditRecord> combobox(VAuditRecord var) {
		return this.changeModel(this.find(var));
	}

	@Override
	public DataGrid datagrid(VAuditRecord var) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(var)));
		dg.setTotal(this.total(var));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					AuditRecord ar = this.auditRecordDao.get(AuditRecord.class, Long.parseLong(id.trim()));
					if (ar != null) {
						auditRecordDao.delete(ar);
					}
				}
			}
		}
	}

	@Override
	public void save(VAuditRecord var) {
		AuditRecord ar = new AuditRecord();
		BeanUtils.copyProperties(var, ar);
		if (var.getRid() != null) {
			ar.setRepertory(new Repertory(var.getRid()));
		}
		if (var.getUid() != null) {
			ar.setUser(new User(var.getUid()));
		}
		this.auditRecordDao.save(ar);
	}

	@Override
	public void update(VAuditRecord var) {
		AuditRecord ar = this.auditRecordDao.get(AuditRecord.class, var.getId());
		BeanUtils.copyProperties(var, ar, new String[] { "id" });
		if (var.getRid() != null) {
			ar.setRepertory(new Repertory(var.getRid()));
		}
		if (var.getUid() != null) {
			ar.setUser(new User(var.getUid()));
		}
		this.auditRecordDao.update(ar);
	}

	private List<VAuditRecord> changeModel(List<AuditRecord> ars) {
		List<VAuditRecord> vars = new ArrayList<VAuditRecord>();
		if (ars != null && ars.size() > 0) {
			for (AuditRecord ar : ars) {
				VAuditRecord var = new VAuditRecord();
				BeanUtils.copyProperties(ar, var);
				if (ar.getRepertory() != null) {
					Repertory repertory = ar.getRepertory();
					var.setRid(repertory.getId());
					var.setRname(repertory.getName());
				}
				if (ar.getUser() != null) {
					User user = ar.getUser();
					var.setUid(user.getId());
					var.setUname(user.getUsername());
				}
				vars.add(var);
			}
		}
		return vars;
	}

	private List<AuditRecord> find(VAuditRecord var) {
		String hql = "from AuditRecord t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(var, hql, values);

		if (var.getSort() != null && var.getOrder() != null) {
			hql += " order by " + var.getSort() + " " + var.getOrder();
		}
		return auditRecordDao.find(hql, values, var.getPage(), var.getRows());
	}

	private Long total(VAuditRecord var) {
		String hql = "select count(*) from AuditRecord t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(var, hql, values);
		return this.auditRecordDao.count(hql, values);
	}

	private String addWhere(VAuditRecord var, String hql, List<Object> values) {
		if (var.getId() != null) {
			hql += " and t.id = ? ";
			values.add(var.getId());
		}
		if (var.getUid() != null) {
			hql += " and t.user.id = ? ";
			values.add(var.getUid());
		}
		if (var.getRid() != null) {
			hql += " and t.repertory.id = ? ";
			values.add(var.getRid());
		}
		return hql;
	}

}
